#!/usr/bin/env python
#coding:utf-8

import time
import datetime

from autorm.model import Model
from autorm.db.query import Query

class Model(Model):
    '''Base class for all model.
    
    @author Julot <andy.julot@gmail.com>
    @since Friday, June 10th, 2011'''
    
    errors = {}
    
    def save(self):
        epoch = time.mktime(datetime.datetime.now().timetuple())
        if self._new_record:
            self.insertTime = epoch
        self.updateTime = epoch
        return super(Model, self).save()
    
    @classmethod
    def createIndex(cls):
        '''Create table index as determine in Meta'''
        
        if hasattr(cls.Meta, 'fields'):
            cmd = 'CREATE INDEX [idx_%s_%s] ON [%s] ([%s] ASC);'
            for f in cls.Meta.fields:
                if f.index:
                    data = (cls.Meta.table, f.name, cls.Meta.table, f.name)
                    Query.raw_sql(cmd % data)
                